<template lang="html">
  <div class="main-page" id="store-page">
    <!-- 标题 -->
    <!-- <van-nav-bar class="main-header" title="会员" :left-arrow="false" :fixed="true" :z-index="11" /> -->
    <!-- <nav-bar :title="'会员'" :hiddenLeft="true"/> -->
    <!-- 主体内容 -->
    <div class="main-content has-header-space">
      <VipPage :asset_info="asset_info" :user_info="user_info" v-if="isVip && !loading" ref="vip_page"></VipPage>
      <UserPage v-if="!isVip && !loading" :invite_info="invite_info" :user_info="user_info"></UserPage>
    </div>
    <!-- 底部 tarbar -->
    <!-- <van-tabbar class="main-tabbar"
      :value="active"
      :z-index="11"
      active-color="#07c160"
    >
       <van-tabbar-item icon="home-o" to="/home" :replace="true">首页</van-tabbar-item>
      <van-tabbar-item icon="shop-o" to="/merber/index">会员</van-tabbar-item>
      <van-tabbar-item icon="user-o" to="/personal_center/index">个人中心</van-tabbar-item>
    </van-tabbar> -->
  </div>
</template>

<script>
import VipPage from '@/components/merber/index/vip_page.vue'
import UserPage from '@/components/merber/index/user_page_three.vue'
// import NavBar from '@/components/global/nav-bar.vue'
import Tools from '@/utils/tool.js'
export default {
  components: { VipPage, UserPage },
  data () {
    return {
      active: 1,
      asset_info: {},
      user_info: {},
      invite_info: [],
      isVip: false,
      loading: true,
      cash_amount: 0
    }
  },
  created () {
    let isVip = sessionStorage.getItem('isVip')
    let assetInfo = sessionStorage.getItem('asset_info')
    let userInfo = sessionStorage.getItem('user_info')
    if (!isVip) {
      sessionStorage.setItem('isVip', false)
      this.isVip = false
    } else {
      if (isVip === 'true') {
        this.isVip = true
      } else {
        this.isVip = false
      }
    }
    if (assetInfo) {
      this.asset_info = JSON.parse(assetInfo)
    }
    if (userInfo) {
      this.user_info = JSON.parse(userInfo)
    }
    let token = sessionStorage.getItem('token')
    if (token) {
      this.getProfile()
    } else {
      this.isVip = false
      this.loading = false
    }
  },
  mounted () {
    // 将backToday方法绑定到window下面，提供给外部调用
    window['getUpload'] = () => {
      this.getProfile()
    }
  },
  methods: {
    getProfile () {
      let isClient = Tools.isClient()
      console.log(isClient)
      return new Promise((resolve, reject) => {
        this.$Axios({ url: 'profile', method: 'get' }).then((resp) => { // 请求成功
          this.loading = false
          if (resp.return_code === '0000') {
            this.asset_info = resp.data.asset_info
            this.user_info = resp.data.user_info
            sessionStorage.setItem('asset_info', JSON.stringify(resp.data.asset_info))
            sessionStorage.setItem('user_info', JSON.stringify(resp.data.user_info))
            if (resp.data.user_info.lev_code === 'COMMON_USER') {
              sessionStorage.setItem('isVip', false)
              document.title = '会员权益'
              this.isVip = false
              if (isClient) {
                window.location.href = 'yggtitle://' + encodeURIComponent('会员权益')
              }
            } else {
              this.isVip = true
              setTimeout(() => {
                this.$refs.vip_page.getTeam()
                if (parseFloat(resp.data.asset_info.real_balance) >= 50) {
                  var newtime = new Date()
                  var tispdate = newtime.toLocaleDateString()
                  let storgtipsdate = localStorage.getItem('tisp_date')
                  if (tispdate !== storgtipsdate) {
                    this.$refs.vip_page.getMyInfo()
                  }
                }
              }, 100)
              document.title = '我的权益'
              sessionStorage.setItem('isVip', true)
              if (isClient) {
                window.location.href = 'yggtitle://' + encodeURIComponent('我的权益')
              }
            }
          }
          resolve(resp)
        }).catch((error) => { // 请求失败
          this.$_requestInterceptor('error', error)
          reject(error)
        })
      })
    }
  }
}
</script>
